//
//  QNPlayerVC.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2019/7/22.
//  Copyright © 2019年 Lssyyy. All rights reserved.
//

#import "QNPlayerVC.h"
#import <PLPlayerKit/PLPlayerKit.h>
@interface QNPlayerVC ()<PLPlayerDelegate>

@property (nonatomic, strong) PLPlayer *player;
@end

@implementation QNPlayerVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initPlayer];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (![self.player isPlaying]) {
        [self.player play];
//        [self starTimer];
//        self.resumeBtn.hidden = YES;
    }
}
- (void)initPlayer{
//    http://vfx.mtime.cn/Video/2019/03/18/mp4/190318231014076505.mp4
    NSString *urlStr = @"http://video.srsjk.com/short_video_20190719104332.mp4";
    PLPlayerOption *option = [PLPlayerOption defaultOption];
    PLPlayFormat format = kPLPLAY_FORMAT_UnKnown;
    NSString *urlString = [urlStr lowercaseString];
    if ([urlString hasSuffix:@"mp4"]) {
        format = kPLPLAY_FORMAT_MP4;
    } else if ([urlString hasPrefix:@"rtmp:"]) {
        format = kPLPLAY_FORMAT_FLV;
    } else if ([urlString hasSuffix:@".mp3"]) {
        format = kPLPLAY_FORMAT_MP3;
    } else if ([urlString hasSuffix:@".m3u8"]) {
        format = kPLPLAY_FORMAT_M3U8;
    }
    [option setOptionValue:@(format) forKey:PLPlayerOptionKeyVideoPreferFormat];
    [option setOptionValue:@(kPLLogNone) forKey:PLPlayerOptionKeyLogLevel];
    _player = [PLPlayer playerWithURL:[NSURL URLWithString:urlStr] option:option];
    [self.view insertSubview:self.player.playerView atIndex:0];
    [self.player.playerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    self.player.delegateQueue = dispatch_get_main_queue();
    self.player.playerView.contentMode = UIViewContentModeScaleAspectFit;
    self.player.delegate = self;
    self.player.loopPlay = YES;
}

#pragma mark - PLPlayerDelegate

- (void)player:(PLPlayer *)player statusDidChange:(PLPlayerStatus)state {
    NSLog(@"播放器状态：%ld",state);
    switch (state) {
        case PLPlayerStatusUnknow://初始化状态
        case PLPlayerStatusPreparing://正在准备播放所需组件
        case PLPlayerStatusReady://播放组件准备完成
        case PLPlayerStatusOpen://播放组件准备完成，准备开始连接
            
//            [self.activityView startAnimating];
            
            break;
            
        case PLPlayerStatusPlaying://播放中
//            [self.activityView stopAnimating];
//            [self starTimer];
            //            self.pauseBtn.hidden = false;
            break;
            
        case PLPlayerStatusPaused://暂停中
            
            break;
            
        case PLPlayerStatusCompleted://播放完成
            break;
        case PLPlayerStatusStopped://停止状态,该状态仅会在回放时播放结束出现，RTMP 直播结束并不会出现此状态
            break;
            
        case PLPlayerStatusError://播放失败
            
            break;
            
        default:
            break;
    }
}

/**
 视频宽高数据回调通知
 
 @param player 调用该方法的 PLPlayer 对象
 @param width 视频流宽
 @param height 视频流高
 
 @since v3.3.0
 */
- (void)player:(nonnull PLPlayer *)player width:(int)width height:(int)height{
    if (width > height) {
        player.playerView.contentMode = UIViewContentModeScaleAspectFit;
    }else{
        player.playerView.contentMode = UIViewContentModeScaleAspectFill;
    }
    
}


@end
